<?php
header("Content-type:text/html; charset=utf-8");
require_once ('config.php');
require_once (WEBURL.'/include/conn.php');
require_once (WEBURL.'/login_status_check.php');
if(!isset($_POST['channel_name'])){
	echo "forbbiden";
	exit();
}
$channel_id=$_POST['channel_id'];

if(!checkLogin()){
	$gotopage=base64_encode("channel_modify.php?channel_id=".$channel_id);
	header("Location:admin_login.php?gotopage=".$gotopage);

}
else{
	$channel_name=$_POST['channel_name'];
	$pre_channel=$_POST['pre_channel'];
	$channel_old_pre_id=$_POST['channel_old_pre_id'];
	$channel_grade=$_POST['pre_channel_grade']+1;
	$whether_channel_has_articles=$_POST['whether_channel_has_articles'];
	$article_move_to_channel=$_POST['article_move_to_channel'];
	$channel_model=$_POST['channel_model'];
	$article_model=$_POST['article_model'];
	$channel_describe=base64_encode($_POST['channel_describe']);
	$sql_channel_modify_save="update channel set pre_id=$pre_channel,channel_grade=$channel_grade,
	channel_name='$channel_name',
	channel_describe='$channel_describe',channel_model='$channel_model',article_model='$article_model' where channel_id=$channel_id;";
	//echo $sql_channel_modify_save;
	$result_channel_modify_save=@mysql_query($sql_channel_modify_save);
	if($result_channel_modify_save){
		/*若父栏目中有文章，将这些文章进行转移操作*/
		if($whether_channel_has_articles==1){
			if($article_move_to_channel==0){
				$article_move_to_channel=$channel_id;
			}
			/*替换，数据库操作*/
			$sql_update_article_channel="update article_normal set article_main_channel=$article_move_to_channel where article_main_channel=$pre_channel;";
			//echo $sql_update_article_channel;
			$result_update_article_channel=mysql_query($sql_update_article_channel) or die(mysql_error());
		}
		
		if($channel_old_pre_id!==$pre_channel&&$channel_old_pre_id!=0){
			/*更新旧的pre_id的is_bottom*/
			$sql_check_old_pre_has_child="select * from channel where pre_id=$channel_old_pre_id";
			//echo $sql_check_old_pre_has_child;
			$result_check_old_pre_has_child=mysql_query($sql_check_old_pre_has_child);
			$num=mysql_num_rows($result_check_old_pre_has_child);
			if($num==0){
				$sql_update_old_pre_channel="update channel set is_bottom=1 where channel_id=$channel_old_pre_id;";
				$result_update_old_pre_channel=mysql_query($sql_update_old_pre_channel) or die(mysql_error());
			}
			
		}

		if($pre_channel!=0){
			/*更新父栏目is_bottom*/
				$sql_update_pre_channel="update channel set is_bottom=0 where channel_id=$pre_channel;";
			//echo $sql_update_pre_channel;
			$result_update_pre_channel=mysql_query($sql_update_pre_channel) or die(mysql_error());
				
		}
		echo "<script language=JavaScript> alert('修改栏目成功！'); window.location=\"channel_show.php\"</script>";
	}

}